﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthGetAction("ETL", "GetDimItemList", "1.0")]
    public class GetDimItemListAction : BaseAction<GetDimItemListParams, GetDimItemListResult>
    {
        protected override ActionResult<GetDimItemListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetDimItemListParams opParams)
        {
            SER.DimService service = new SER.DimService();
            PagedList<ENT.EDimItem> serResult = service.GetDimItemList(opParams.DimId, opParams.IncludeOverdue, opParams.Page * 25, 25);
            if(serResult.IsSuccess)
            {
                GetDimItemListResult result = new GetDimItemListResult();

                result.Start = serResult.Start;
                result.Limit = serResult.Limit;
                result.Total = serResult.Total;
                result.Page = opParams.Page;

                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetDimItemListResult.Item item = new GetDimItemListResult.Item();
                    item.Id = serResult.Data[index].Id;
                    item.Code = serResult.Data[index].Code;
                    item.Name = serResult.Data[index].Name;
                    item.EffectiveDate = serResult.Data[index].EffectiveDate;
                    item.ExpiryDate = serResult.Data[index].ExpiryDate;
                    result.Items.Add(item);
                }
                return new ActionResult<GetDimItemListResult>(result);
            }
            else
            {
                return new ActionResult<GetDimItemListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetDimItemListParams
    {
        [Parameter]
        public Guid DimId { get; set; }

        public bool IncludeOverdue { get; set; }

        public int Page { get; set; }
    }

    public class GetDimItemListResult : PagedResult
    {
        public List<Item> Items = new List<Item>();
        public GetDimItemListResult()
        {

        }

        public class Item
        {
            public Guid Id { get; set; }
            public string Code { get; set; }

            public string Name { get; set; }

            public DateTime EffectiveDate { get; set; }

            public DateTime ExpiryDate { get; set; }
        }
    }
}
